﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuickGame.Model;

namespace QuickGame.View
{
    public partial class QLLuotChoi : System.Web.UI.Page
    {
        private ModelContext modelContext;
        private long idnguoidung ;
        private long idcauhoi;
        private long idcautraloi;
        private long idluotchoi;
        private long idchitietchoi;
        private ChiTietChoi chitietchoi;
        private LuotChoi luotchoi;
        private CauHoi cauhoi;
        private int diem;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.LoadDL();
            }
        }
        #region Load dữ liệu
        public void LoadDL()
        {

            this.modelContext = new ModelContext();
            if (Session["idnguoidung"] == null)
            {
                Response.Redirect("/Account/Login.aspx");
            }
            else
            {
                long idnguoidung = (long)Session["idnguoidung"];
                var lisluotchoi = LuotChoiHs.QueryEntities(modelContext).Where(k => k.IDNguoiDung == idnguoidung).OrderByDescending(k => k.ID);
                luotchoi = lisluotchoi.FirstOrDefault();
                if (luotchoi == null)
                {
                    this.txtEror.Text = "Bạn chưa có lượt chơi nào! Hãy bắt đầu 1 lượt mới";
                    this.cblCauTraLoi.Visible = false;
                    this.btnSuDungTroGiup.Enabled = false;
                    this.btnTiepTuc.Enabled = false;
                    this.btnTraLoi.Enabled = false;
                }
                else
                {
                    txtthongtinnguoichoi.Text = "Người chơi : "+ luotchoi.NguoiDung.Ten;
                    chitietchoi = luotchoi.ChiTietChois.OrderByDescending(k => k.ID).FirstOrDefault();
                    idchitietchoi = chitietchoi.ID;
                    idluotchoi = luotchoi.ID;
                    cauhoi = chitietchoi.CauHoi;
                    idcauhoi = cauhoi.ID;
                    txtloaicauhoi.Text = "Câu hỏi số " + cauhoi.IDCapDo.ToString() + " : ";
                    txtCauHoi.Value = cauhoi.Ten;
                    string a = "";
                    if (luotchoi.TrangThai == false)
                    {
                        a = " (Hoàn tất)";
                    }
                    else
                    {
                        a = " (Đang chơi)";
                    }
                    txtThongtinluotchoi.Text = "Lượt chơi ngày: " + string.Format("{0:dd/MM/yyyy hh:mm}", luotchoi.NgayBatDau) + a;
                    if (luotchoi.Diem == null)
                    {
                        diem = 0;
                    }
                    else
                    {
                        diem = luotchoi.Diem.Value;
                    }
                    txtTongtien.Text = diem.ToString();
                    listcautraloi.Items.Clear();
                    var cautralois = CauTraLoiHs.QueryEntities(modelContext).Where(k => k.IDCauHoi == cauhoi.ID).Select(k => new CauTraLoiTrongLuotChoiView
                    {

                        Ten = k.Ten,
                        ID = k.ID,
                    });
                    if (cautralois.Count() > 0)
                    {
                        var i = -1;
                        foreach (var cautraloi in cautralois)
                        {
                            listcautraloi.Items.Add(cautraloi.Ten, cautraloi.ID);
                            i += 1;
                            cblCauTraLoi.Items[i].Text = cautraloi.Ten;
                            cblCauTraLoi.Items[i].Value = cautraloi.ID.ToString();
                        }
                    }
                    listcautraloi.Visible = true;
                    cblCauTraLoi.Visible = false;
                    listcautraloi.DataBind();
                    if (lisluotchoi.Count() > 0)
                    {
                        var lichsuchoi = lisluotchoi.Select(k => new LuotChoiView
                        {
                            ID = k.ID,
                            Ngay = k.NgayBatDau,
                            CauTraLoi = k.ChiTietChois.Count(),
                            Diem = k.Diem == null ? 0 : k.Diem.Value
                        }).OrderByDescending(k => k.Ngay);
                        grvLichsuchoi.DataSource = lichsuchoi;
                        grvLichsuchoi.DataBind();
                    }
                    this.UpdateView(luotchoi, chitietchoi);
                    Session["idnguoidung"] = idnguoidung;
                    Session["idcauhoi"] = idcauhoi;
                    Session["idcautraloi"] = idcautraloi;
                    Session["idluotchoi"] = idluotchoi;
                    Session["idchitietchoi"] = idchitietchoi;
                    Session["diem"] = diem;
                }

            }

        }
        #endregion
        
        #region Update view
        public void UpdateView(LuotChoi luotchoi, ChiTietChoi chitietchoi)
        {
            this.rb5050.ClientEnabled = true;
            this.rbFlip.ClientEnabled = true;
            this.rb2dapan.ClientEnabled = true;
            foreach (var chitiet in luotchoi.ChiTietChois)
            {
                if (chitiet.IDTroGiup == 1)
                {
                    this.rb5050.ClientEnabled = false;
                }
                else if (chitiet.IDTroGiup == 2)
                {
                    this.rbFlip.ClientEnabled = false;
                }
                else if (chitiet.IDTroGiup == 3)
                {
                    this.rb2dapan.ClientEnabled = false;
                }
            }
            if (chitietchoi.TrangThai == true)
            {
                this.listcautraloi.ClientEnabled = true;
                this.cblCauTraLoi.Enabled = true;
                if (chitietchoi.IDTroGiup != null)
                {
                    this.btnSuDungTroGiup.Enabled = false;
                    this.rb2dapan.ClientEnabled = false;
                    this.rb5050.ClientEnabled = false;
                    this.rbFlip.ClientEnabled = false;
                    if (chitietchoi.IDTroGiup == 1)
                    {
                        this.rb5050.Checked = true;
                        TroGiup5050(chitietchoi);
                    }
                    else if (chitietchoi.IDTroGiup == 2)
                    {
                        this.rbFlip.Checked = true;
                    }
                    else if (chitietchoi.IDTroGiup == 3)
                    {
                        cblCauTraLoi.Visible = true;
                        listcautraloi.Visible = false;
                    }
                }
                else
                {
                    btnSuDungTroGiup.Enabled = true;
                }
                if (luotchoi.TrangThai == true)
                {
                    this.btnTraLoi.Enabled = true;
                    this.btnTiepTuc.Enabled = false;
                }
            }
            else
            {
                if (luotchoi.TrangThai == true)
                {
                    this.btnTraLoi.Enabled = false;
                    this.btnSuDungTroGiup.Enabled = false;
                    this.listcautraloi.ClientEnabled = false;
                    this.cblCauTraLoi.Enabled = false;
                    this.btnTiepTuc.Enabled = true;
                }
                else
                {
                    this.btnSuDungTroGiup.Enabled = false;
                    this.listcautraloi.ClientEnabled = false;
                    this.cblCauTraLoi.Enabled = false;
                    this.btnTraLoi.Enabled = false;
                    this.btnTiepTuc.Enabled = false;
                }
                if (chitietchoi.IDTroGiup == 3)
                {
                    cblCauTraLoi.Visible = true;
                    listcautraloi.Visible = false;
                }
            }
        }
        #endregion
        
        #region Trả lời
        protected void ASPxButton1_Click(object sender, EventArgs e)
        {
            this.modelContext = new ModelContext();
            if (Session["idchitietchoi"] != null)
            {
                long idchitietchoi = (long)Session["idchitietchoi"];
                chitietchoi = ChiTietChoiHs.QueryEntity(modelContext, idchitietchoi);
                if (chitietchoi.IDTroGiup == 3)
                {
                    if (Check2cautraloi())
                    {
                        long a1 = 0;
                        long a2 = 0;
                        for (int i = 0; i < 4; i++)
                        {
                            if (cblCauTraLoi.Items[i].Selected == true)
                            {
                                if (a1 == 0)
                                {
                                    a1 = Convert.ToInt64(cblCauTraLoi.Items[i].Value);
                                }
                                else
                                {
                                    a2 = Convert.ToInt64(cblCauTraLoi.Items[i].Value);
                                }
                            }
                        }
                        var cautraloi1 = CauTraLoiHs.QueryEntity(modelContext, a1);
                        var cautraloi2 = CauTraLoiHs.QueryEntity(modelContext, a2);
                        if (cautraloi1.TrangThai == true && cautraloi2.TrangThai == false)
                        {
                            chitietchoi.CauTraLoi = cautraloi1;
                            chitietchoi.Ngay = DateTime.Now;
                            chitietchoi.TrangThai = false;
                            long ketqua = 1;
                            txtEror.Text = "Bạn đã trả lời đúng";
                            long capdo = (long)cautraloi1.CauHoi.IDCapDo;
                            diem = Tinhdiem((int)Session["diem"], capdo, ketqua);
                            if (capdo < 13)
                            {
                                luotchoi = chitietchoi.LuotChoi;
                                luotchoi.Diem = diem;
                            }
                            else if (capdo >= 13)
                            {
                                luotchoi = chitietchoi.LuotChoi;
                                luotchoi.Diem = diem;
                                luotchoi.TrangThai = false;
                            }
                            modelContext.SaveChanges();
                            txtTongtien.Text = luotchoi.Diem.ToString();
                        }
                        else if (cautraloi1.TrangThai == false && cautraloi2.TrangThai == true)
                        {
                            chitietchoi.CauTraLoi = cautraloi2;
                            chitietchoi.Ngay = DateTime.Now;
                            chitietchoi.TrangThai = false;
                            long ketqua = 1;
                            txtEror.Text = "Bạn đã trả lời đúng";
                            long capdo = (long)cautraloi2.CauHoi.IDCapDo;
                            diem = Tinhdiem((int)Session["diem"], capdo, ketqua);
                            if (capdo < 13)
                            {
                                luotchoi = chitietchoi.LuotChoi;
                                luotchoi.Diem = diem;
                            }
                            else if (capdo >= 13)
                            {
                                luotchoi = chitietchoi.LuotChoi;
                                luotchoi.Diem = diem;
                                luotchoi.TrangThai = false;
                            }
                            modelContext.SaveChanges();
                            txtTongtien.Text = luotchoi.Diem.ToString();
                        }
                        else if (cautraloi1.TrangThai == false && cautraloi2.TrangThai == false)
                        {
                            chitietchoi.CauTraLoi = cautraloi1;
                            chitietchoi.Ngay = DateTime.Now;
                            chitietchoi.TrangThai = false;
                            long ketqua = 0;
                            txtEror.Text = "Bạn đã trả lời sai";
                            long capdo = (long)cautraloi1.CauHoi.IDCapDo;
                            diem = Tinhdiem((int)Session["diem"], capdo, ketqua);
                            luotchoi = chitietchoi.LuotChoi;
                            luotchoi.Diem = diem;
                            luotchoi.TrangThai = false;
                            modelContext.SaveChanges();
                            txtTongtien.Text = luotchoi.Diem.ToString();
                        }
                        this.UpdateView(luotchoi, chitietchoi);
                    }
                    else
                    {
                        txtEror.Text = "Bạn phải chọn 2 đáp án";
                    }
                }
                else
                {
                    if (listcautraloi.SelectedIndex != -1)
                    {
                        var a = listcautraloi.SelectedItem.Value;
                        long idcautraloi = Convert.ToInt64(a);
                        var cautraloi = CauTraLoiHs.QueryEntity(modelContext, idcautraloi);
                        chitietchoi.CauTraLoi = cautraloi;
                        chitietchoi.Ngay = DateTime.Now;
                        chitietchoi.TrangThai = false;

                        long ketqua = 0;
                        if (cautraloi.TrangThai == true)
                        {
                            ketqua = 1;
                            txtEror.Text = "Bạn đã trả lời đúng";
                        }
                        else if (cautraloi.TrangThai == false || cautraloi.TrangThai == null)
                        {
                            ketqua = 0;
                            txtEror.Text = "Bạn đã trả lời sai";
                        }
                        long capdo = (long)cautraloi.CauHoi.IDCapDo;
                        diem = Tinhdiem((int)Session["diem"], capdo, ketqua);
                        if (ketqua == 1 && capdo < 13)
                        {
                            luotchoi = chitietchoi.LuotChoi;
                            luotchoi.Diem = diem;
                        }
                        else if (ketqua == 0 || capdo >= 13)
                        {
                            luotchoi = chitietchoi.LuotChoi;
                            luotchoi.Diem = diem;
                            luotchoi.TrangThai = false;
                        }
                        modelContext.SaveChanges();
                        txtTongtien.Text = luotchoi.Diem.ToString();
                        this.UpdateView(luotchoi, chitietchoi);
                    }
                    else
                    {
                        txtEror.Text = "Bạn chưa chọn câu trả lời";
                    }
                }
            }
        }
        #endregion

        #region Tạo 1 chi tiết chơi mới
        public void TaoLuotChoi(long capdo,LuotChoi luotchoi)
        {
            Random random = new Random();
            var listcauhoi  = CauHoiHs.QueryEntities(modelContext).Where(k => k.IDCapDo == (capdo + 1)).ToList();
            cauhoi = listcauhoi.OrderBy(k => random.Next()).FirstOrDefault();
            chitietchoi = ChiTietChoiHs.Insert(modelContext);
            chitietchoi.IDCauHoi = cauhoi.ID;
            chitietchoi.LuotChoi = luotchoi;
            chitietchoi.Ngay = DateTime.Now;
            chitietchoi.TrangThai = true;
            modelContext.SaveChanges();
        }
        #endregion

        #region Tính điểm
        public int Tinhdiem(int tong, long capdo, long ketqua)
        {
            switch (capdo)
            {
                case 1:
                    if (ketqua == 1)
                    {
                        tong = 50;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 0;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 0;
                    }
                    break;
                case 2:
                    if (ketqua == 1)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 0;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 50;
                    }
                    break;
                case 3:
                    if (ketqua == 1)
                    {
                        tong = 200;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 2)
                    {
                        tong =100;
                    }
                    break;
                case 4:
                    if (ketqua == 1)
                    {
                        tong = 400;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 2)
                    {
                        tong =200;
                    }
                    break;
                case 5:
                    if (ketqua == 1)
                    {
                        tong = 800;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 400;
                    }
                    break;
                case 6:
                    if (ketqua == 1)
                    {
                        tong = 1600;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 800;
                    }
                    break;
                case 7:
                    if (ketqua == 1)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 100;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 1600;
                    }
                    break;
                case 8:
                    if (ketqua == 1)
                    {
                        tong = 6400;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 3200;
                    }
                    break;
                case 9:
                    if (ketqua == 1)
                    {
                        tong = 12500;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 6400;
                    }
                    break;
                case 10:
                    if (ketqua == 1)
                    {
                        tong = 25000;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 12500;
                    }
                    break;
                case 11:
                    if (ketqua == 1)
                    {
                        tong = 50000;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 25000;
                    }
                    break;
                case 12:
                    if (ketqua == 1)
                    {
                        tong = 100000;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 50000;
                    }
                    break;
                case 13:
                    if (ketqua == 1)
                    {
                        tong = 500000;
                    }
                    else if (ketqua == 0)
                    {
                        tong = 3200;
                    }
                    else if (ketqua == 2)
                    {
                        tong = 100000;
                    }
                    break;
                default:
                    break;
            }
            return tong;
        }
        #endregion

        #region Tiếp tục
        protected void btnTiepTuc_Click(object sender, EventArgs e)
        {
            modelContext = new ModelContext();
            txtEror.Text = "";
            if (Session["idchitietchoi"] != null)
            {
                long idchitietchoi = (long)Session["idchitietchoi"];
                chitietchoi = ChiTietChoiHs.QueryEntity(modelContext, idchitietchoi);
                long capdo = (long)chitietchoi.CauHoi.IDCapDo;
                if (capdo < 13)
                {
                    this.TaoLuotChoi(capdo, chitietchoi.LuotChoi);
                }
                this.LoadDL();
            }
        }
        #endregion

        #region Tạo lượt chơi mới
        protected void btnLuotmoi_Click(object sender, EventArgs e)
        {
            modelContext = new ModelContext();
            txtEror.Text = "";
            if (Session["idchitietchoi"] != null)
            {
                long idchitietchoi = (long)Session["idchitietchoi"];
                chitietchoi = ChiTietChoiHs.QueryEntity(modelContext, idchitietchoi);
                cauhoi = chitietchoi.CauHoi;
                if (chitietchoi.TrangThai == true)
                {
                    chitietchoi.TrangThai = false;
                }
                long capdo = (long)cauhoi.IDCapDo;
                luotchoi = chitietchoi.LuotChoi;
                if (chitietchoi.IDCauTraLoi == null)
                {
                    diem = Tinhdiem((int)Session["diem"], capdo, 2);
                    luotchoi.Diem = diem;
                }
                luotchoi.NgayKetThuc = DateTime.Now.ToShortDateString();
                luotchoi.TrangThai = false;
                modelContext.SaveChanges();
                luotchoi = LuotChoiHs.Insert(modelContext);
                long idnguoidung = (long)Session["idnguoidung"];
                luotchoi.IDNguoiDung = idnguoidung;
                luotchoi.NgayBatDau = DateTime.Now;
                luotchoi.TrangThai = true;
                this.TaoLuotChoi(0, luotchoi);
                modelContext.SaveChanges();
                this.rb2dapan.ClientEnabled = true;
                this.rb5050.ClientEnabled = true;
                this.rbFlip.ClientEnabled = true;
                this.LoadDL();
            }
            else
            {
                if (Session["idnguoidung"] == null)
                {
                    Response.Redirect("/Account/Login.aspx");
                }
                else
                {
                    long idnguoidung = (long)Session["idnguoidung"];
                    var lisluotchoi = LuotChoiHs.QueryEntities(modelContext).Where(k => k.IDNguoiDung == idnguoidung).OrderByDescending(k => k.ID);
                    luotchoi = lisluotchoi.FirstOrDefault();
                    if (luotchoi == null)
                    {
                        luotchoi = LuotChoiHs.Insert(modelContext);
                        luotchoi.IDNguoiDung = idnguoidung;
                        luotchoi.NgayBatDau = DateTime.Now;
                        luotchoi.TrangThai = true;
                        this.TaoLuotChoi(0, luotchoi);
                        modelContext.SaveChanges();
                    }
                    this.LoadDL();
                } 
            }

        }
        #endregion  

        #region TroGiup
        public void TroGiup5050(ChiTietChoi chitietchoi)
        {
            Random random = new Random();
            listcautraloi.Items.Clear();
            cauhoi = chitietchoi.CauHoi;
            var cautralois = cauhoi.CauTraLois.ToList();
            var cau1 = cautralois.OrderBy(k => random.Next()).FirstOrDefault();
            if (cau1.TrangThai == true)
            {
                var idcau1 = cau1.ID;
                var cau2 = cautralois.Where(k => k.ID != idcau1).OrderBy(k => random.Next()).FirstOrDefault();
                listcautraloi.Items.Add(cau1.Ten, cau1.ID);
                listcautraloi.Items.Add(cau2.Ten, cau2.ID);
            }
            else if (cau1.TrangThai == false || cau1.TrangThai == null)
            {
                var cau2 = cautralois.Where(k => k.TrangThai == true).FirstOrDefault();
                listcautraloi.Items.Add(cau1.Ten, cau1.ID);
                listcautraloi.Items.Add(cau2.Ten, cau2.ID);
            }
        }
        public void TroGiupDupble(ChiTietChoi chitietchoi)
        {
            
        }
        public bool Check2cautraloi()
        {
            int a = 0;
            for (int i = 0; i < 4; i++)
            {
                if (cblCauTraLoi.Items[i].Selected == true)
                {
                    a += 1;
                }
            }
            if (a == 2)
            {
                return true;
            }
            return false;
        }
        #endregion

        #region Sử dụng trợ giúp
        protected void btnSuDungTroGiup_Click(object sender, EventArgs e)
        {
            modelContext = new ModelContext();
            if (Session["idchitietchoi"] != null)
            {
                long idchitietchoi = (long)Session["idchitietchoi"];
                chitietchoi = ChiTietChoiHs.QueryEntity(modelContext, idchitietchoi);
                luotchoi = chitietchoi.LuotChoi;
                if (this.rb5050.Checked == true)
                {
                    chitietchoi.IDTroGiup = 1;
                    modelContext.SaveChanges();
                }
                else if (this.rbFlip.Checked == true)
                {
                    chitietchoi.IDTroGiup = 2;
                    chitietchoi.TrangThai = false;
                    long capdo = (long)chitietchoi.CauHoi.IDCapDo;
                    diem = Tinhdiem((int)Session["diem"], capdo, 1);
                    if (capdo < 12)
                    {
                        luotchoi.Diem = diem;
                    }
                    else
                    {
                        luotchoi.Diem = diem;
                        luotchoi.TrangThai = false;
                    }
                    modelContext.SaveChanges();
                }
                else if (this.rb2dapan.Checked == true)
                {
                    chitietchoi.IDTroGiup = 3;
                    modelContext.SaveChanges();
                }
                LoadDL();
            }


        }
        #endregion

        protected void grvLichsuchoi_StartRowEditing(object sender, DevExpress.Web.Data.ASPxStartRowEditingEventArgs e)
        {

        }
        
    }
}